//
//  SwipeView.m
//  bApp
//
//  Created by Guglielmo Buzzelli on 16/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SwipeView.h"

@implementation SwipeView
@synthesize imageToSwipe;
@synthesize pageControl;
@synthesize swipeField = _swipeField;
@synthesize swipeFieldBack = _swipeFieldBack;
@synthesize imageList = _imageList;




-(void)awakeFromNib{
    
    
    
    imageList = [[NSMutableArray alloc] initWithObjects:        
                 [UIImage imageNamed:@"1_1.jpg"],
                 [UIImage imageNamed:@"2_2.jpg"],
                 [UIImage imageNamed:@"3_3.jpg"],
                 [UIImage imageNamed:@"4_4.jpg"],
                 [UIImage imageNamed:@"5_5.jpg"],nil
                 ];
    
    
    num_images = [imageList count];
    count_swipe = 0;
    count_pager = 1;
    [pageControl setNumberOfPages:num_images];
    
    imageToSwipe.image = [imageList objectAtIndex:0];
    
    /*UIImage *myImage = [UIImage imageNamed:@"gallery_box_app_empty@2x.png"];
    CGRect cropRect = CGRectMake(10.0, 10.0, 400, 400.0);
    CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
    [myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 
    CGImageRelease(croppedImage);
    
    UIImageView *newIv = [[UIImageView alloc] initWithFrame:[imageToSwipe frame] ];
    newIv.image = myImageView.image;
    [self.imageToSwipe addSubview:newIv];*/
    
    UIImage *shadow = [UIImage imageNamed:@"gallery_box_app_empty@2x.png"];
    
    //UIImageView *newIv = [[UIImageView alloc] initWithFrame:[imageToSwipe frame] ];
    UIImageView *newIv = [[UIImageView alloc] initWithFrame:CGRectMake(-4, -4, 295, 270) ];
    newIv.image = shadow;

    [self.imageToSwipe addSubview:newIv];
    
    
    //[self.imageToSwipe addSubview:[[UIImageView alloc] initWithImage:shadow]];
    
    
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(IBAction)sfogliami:(UISwipeGestureRecognizer *)recognizer{
    
    
    
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    
    
    if(count_swipe > num_images-1){
        count_swipe = 0;
    }


    imageToSwipe.image = [imageList objectAtIndex:count_swipe];

    
    if(count_pager > num_images-1){
        count_pager = 0;
    }
    [pageControl setNumberOfPages:num_images];
    
    [pageControl setCurrentPage:count_pager];
    
    [pageControl updateCurrentPageDisplay];
    
    count_swipe++;
    count_pager++;

    
}




-(IBAction)sfogliamiBack:(UISwipeGestureRecognizer *)recognizerBack{
    
    
    
    if(count_swipe > num_images-1){
        count_swipe = 0;
    }else if(count_swipe == 0){
        count_swipe = num_images-1;
    }
    
    //imageToSwipe.image =[UIImage imageNamed:[imageList objectAtIndex:count_swipe]];  
    imageToSwipe.image =[imageList objectAtIndex:count_swipe];  
    
    if(count_pager > num_images-1){
        count_pager = 0;
    }else if(count_pager == 0){
        count_pager = num_images-1;
    }
    [pageControl setNumberOfPages:num_images];
    
    [pageControl setCurrentPage:count_pager];
    
    [pageControl updateCurrentPageDisplay];
    
    count_swipe--;
    count_pager--;
           NSLog(@"ci sono <-"); 

    
}

@end
